package org.kunlong.netix.boot;

import org.apache.log4j.Logger;
import org.kunlong.netix.Server;
import org.kunlong.netix.config.ServerConfigManager;
import org.kunlong.netix.container.Container;
import org.kunlong.netix.container.standcontainer.StandardContainer;
import org.kunlong.netix.exception.LifeCycleException;
import org.kunlong.netix.listner.Listner;
import org.kunlong.netix.listner.standardlistner.StandardListner;
import org.kunlong.netix.processor.Processor;
import org.kunlong.netix.processor.standardprocessor.StandardProcessor;
import org.kunlong.netix.standardserver.StandServer;
import org.kunlong.netix.util.log.stringmanager.StringManager;

public class BootStrap {
	private static String PACKAGE_NAME = BootStrap.class.getPackage().getName();
	private static Logger logger = Logger.getLogger(BootStrap.class);
	private static String SERVER_INIT_CONFIG_ERROR = "server_init_config_error";
	private static String SERVER_INIT_ERROR = "server_init_error";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			ServerConfigManager.initConfig();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			String error = StringManager.getLogString(PACKAGE_NAME,
					SERVER_INIT_CONFIG_ERROR);
			logger.error(error, e);
			System.exit(1);
		}
		Server server = new StandServer();
		Processor processor = new StandardProcessor();
		Listner listner = new StandardListner();
		listner.setPort(80);
		Container container = new StandardContainer();
		server.addContainer(container);
		server.addListner(listner);
		server.addProcessor(processor);
		try {
			server.init();
		} catch (LifeCycleException e) {
			// TODO: handle exception
			e.getException().printStackTrace();
			String error = StringManager.getLogString(PACKAGE_NAME,
					SERVER_INIT_ERROR);
			logger.error(error, e.getException());
			System.exit(1);
		}
		try {
			server.start();//normally there is no exception will be throw out
		} catch (LifeCycleException e) {
			// TODO: handle exception
			e.getException().printStackTrace();
			logger.error(e.getException().getMessage(), e.getException());
		}
	}
}
